package cn.hxlc.tie.demo.modular.station.service.impl;/*
 @author rui
 @date 2021/6/11 --12:12
*/

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.modular.station.entity.User;
import cn.hxlc.tie.demo.modular.station.mapper.UserMapper;
import cn.hxlc.tie.demo.modular.station.model.param.UserParam;
import cn.hxlc.tie.demo.modular.station.service.UserService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
   private UserMapper userMapper;

    @Override
    public Boolean add(UserParam userParam) {
        User user = new User();
        BeanUtil.copyProperties(userParam,user);
       return baseMapper.insert(user)>0;
    }

    @Override
    public List<User> findAll(UserParam userParam) {

        return baseMapper.findAll(userParam);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByOne(UserParam userParam) {
        UpdateWrapper wrapper=new UpdateWrapper<>();
        wrapper.eq("id",userParam.getId());
        wrapper.set("address",userParam.getAddress());
        wrapper.set("name",userParam.getName());
        User user=new User();
        BeanUtil.copyProperties(userParam,user);
        return baseMapper.update(user,wrapper)>0;
    }

    @Override
    public List<User> findDate(int id) {
        return userMapper.findDate(id);
    }

    @Override
    public Boolean updateDate(UserParam userParam) {
        UpdateWrapper wrapper = new UpdateWrapper<User>();
        wrapper.eq("id",userParam.getId());
        wrapper.set("date",userParam.getDate());
        return baseMapper.update(null,wrapper)>0;
    }

    @Override
    public Boolean batchDelete(List<Integer> ids) {
        return baseMapper.batchDelete(ids);

    }

    @Override
    public Boolean delete(int id) {
        int i= baseMapper.delete(id);
        return i>0;
    }

    @Override
    public PageResult<User> queryListPage(UserParam userParam) {
        Page<User> page = PageFactory.defaultPage();
        IPage<User> resultPage = userMapper.queryListPage(page, userParam);
        return new PageResult<>(resultPage);
    }

    @Override
    public List<User> findByOne(int id) {
        return baseMapper.findByOne(id);
    }
}



